package com.wanglei.accumulator

import org.apache.spark.{SparkConf, SparkContext}

// 累加器：分布式共享只写变量
object Spark01_acc {

  def main(args: Array[String]): Unit = {

    //
    val conf = new SparkConf().setMaster("local[2]").setAppName("acc")
    val sc = new SparkContext(conf)


    //
    val rdd = sc.makeRDD(List(
      1, 2, 3, 4
    ))

    val sum = sc.longAccumulator("sum")
    rdd.foreach(
      num => sum.add(num)
    )

    println(sum.value)


    //
    sc.stop()

  }
}
